package com.heitian.ssm.dao.split;

import org.apache.log4j.Logger;

/**
 * @创建者：zzw
 * @创建时间 2019/3/7 0007
 * @描述 数据源管理类
 */

public class DynamicDataSourceHolder {

    private static Logger log = Logger.getLogger(DynamicDataSourceHolder.class);
    private static ThreadLocal<String> contextHolder = new ThreadLocal<String>();
    public static final String DB_MASTER = "master";
    public static final String DB_SLAVE = "slave";

    /**
     * 获取数据源
     * @return
     */
    public static String getDbType() {
        String db = contextHolder.get();
        if(db==null) {
            db = DB_MASTER;
        }
        return db;
    }

    /**
     * 设置数据源
     * @param dbType
     */
    public static void setDbType(String dbType) {
        log.debug("所使用的数据源"+dbType);
        contextHolder.set(dbType);
    }

    /**
     * 清理数据源
     */
    public static void clearDbType() {
        contextHolder.remove();
    }

}